﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JasRibbonApplicatie.Classes;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using JasRibbonApplicatie.Model;

namespace JasRibbonApplicatie.Views
{
    /// <summary>
    /// Interaction logic for BeheerMateriaal.xaml
    /// </summary>
    public partial class BeheerMateriaal : Page
    {
        List<int> toebehorenIdList = new List<int>();
        private String URL = Properties.Settings.Default.HTTP_BASEPATH;
        private Page currentPage;

        private static MateriaalModel mm = new MateriaalModel();

        public BeheerMateriaal()
        {
            InitializeComponent();
            FillNodes();

            overzichtFrame.NavigationService.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(NavigationService_LoadCompleted);
            overzichtFrame.Source = new Uri("Omschrijving.xaml", UriKind.Relative);

        }

        public void FillNodes()
        {
            List<Materiaaltype> items = null;
            TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            Task.Factory.StartNew(() =>
            {
                items = mm.GetAllMateriaalType();
            }
            ).ContinueWith((task) =>
            {
                if (task.IsFaulted) MessageBox.Show(task.Exception.ToString());
                Task.Factory.StartNew(() =>
                {
                    materiaalTreeView.ItemsSource = items;
                }, CancellationToken.None, TaskCreationOptions.None, uiScheduler);
            });
        }

        public void ButtonChoice(string buttonName)
        {
            String uri = string.Empty;
            switch (buttonName)
            {
                case "btnshowInfo":
                    showOmschrijving(materiaalTreeView.SelectedItem);
                    break;
                case "btnNieuwMateriaal":
                    uri = "MateriaalToevoegen.xaml";
                    break;
                case "btnCombineMaterials":
                    uri = "ToebehorenLinkMateriaal.xaml";
                    break;
                case "btnAddToebehoren":
                    uri = "ToebehorenToevoegen.xaml";
                    break;
            }

            if (uri != string.Empty)
                overzichtFrame.Source = new Uri(uri, UriKind.Relative);

        }

        private void showOmschrijving(object item)
        {
            if (item is Materiaaltype)
            {
                if (currentPage is MateriaalOmschrijving)
                {
                    (currentPage as MateriaalOmschrijving).OmschrijvingMateriaal(item as Materiaaltype);
                }
                else
                {
                    overzichtFrame.Source = new Uri("MateriaalOmschrijving.xaml", UriKind.Relative);
                }
                
            }
            else if (item is ToebehorenType)
            {
                if (currentPage is ToebehorenOmschrijving)
                {
                    (currentPage as ToebehorenOmschrijving).OmschrijvingToebehoren(item as ToebehorenType);
                }
                else
                {
                    overzichtFrame.Source = new Uri("ToebehorenOmschrijving.xaml", UriKind.Relative);
                }
            }
            else
            {
                overzichtFrame.Source = new Uri("Omschrijving.xaml", UriKind.Relative);
            }

        }

        private void materiaalTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            showOmschrijving(materiaalTreeView.SelectedItem);
        }

        void NavigationService_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            currentPage = ((Page)e.Content);
        }

        private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = GetTreeViewItemFromContextMenu(sender as FrameworkElement, materiaalTreeView);
            if (item != null)
            {
                ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(item);
                if (parent != null)
                {
                    mm.RemoveToebehorenTypeFromMateriaalType(parent.DataContext as Materiaaltype, item.DataContext as ToebehorenType);
                    FillNodes();
                }
            }
        }
        private TreeViewItem GetTreeViewItemFromContextMenu(FrameworkElement sender, TreeView treeView)
        {
            Point menuClickPoint = (sender.Parent as ContextMenu).TranslatePoint(new Point(0, 0), treeView);

            // get the first potential treeView object that was hit   
            DependencyObject obj = treeView.InputHitTest(menuClickPoint) as DependencyObject;

            // cycle up the tree until you hit a TreeViewItem   
            while (obj != null && !(obj is TreeViewItem))
            {
                obj = VisualTreeHelper.GetParent(obj);
            }

            return obj as TreeViewItem;
        } 


    }
}
